# coding:utf-8

projects = {
    'ipad': {'name': 'ipad', 'price': 2200, 'desc': '平板电脑'},
    'iphone': {'name': 'iphone', 'price': 3000, 'desc': '智能手机'},
    'pc': {'name': 'pc', 'price': 5000, 'desc': '台式电脑'},
    'mac': {'name': 'mac', 'price': 8000, 'desc': '平板电脑'}
}

print(projects.keys())

print('一个中学生购买了{}，价格是{}'.format(projects['pc']['name'], projects['pc']['price']))
'''
    pop(key[, default])
    如果 key 存在于字典中则将其移除并返回其值，否则返回 default。 如果 default 未给出且 key 不存在于字典中，则会引发 KeyError。
'''
projects.pop('pc')
print(projects.keys())  # dict_keys(['ipad', 'iphone', 'pc', 'mac'])

result = projects.pop('mac')
print('一个程序员购买了{}, 它的价格是{}'.format(result['name'], result.get('price')))  # 一个中学生购买了pc，价格是5000
print(projects.keys())  # dict_keys(['ipad', 'iphone', 'mac'])

print('{} 和 {} 都被卖出了， 他们一共花费了{}元'.format(
    projects['ipad']['name'], projects['iphone']['name'],
    projects['ipad']['price'] + projects['iphone']['price']
))  # 一个程序员购买了mac, 它的价格是8000
projects.clear()
print(projects.keys())  # dict_keys([])

del projects
print(projects)  # NameError: name 'projects' is not defined. Did you mean: 'property'?
